﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace DRSM
{
    class PhysicalAidControl
    {

        public static void AddPhysicalAid(TextBox tbxItemNameAddPhyscialAid, TextBox tbxQuantity, RichTextBox rtbDescriptionAddPhyscialAid,
        RadioButton rdnBtnFood, RadioButton rdnBtnClothes, RadioButton rdnBtnBlanket)
        {
         //count++;         
            
            string itemName = tbxItemNameAddPhyscialAid.Text.Trim();
            string itemQuantity = tbxQuantity.Text.Trim();
            string itemDesc = rtbDescriptionAddPhyscialAid.Text.Trim();


            string type = "";
            if (rdnBtnFood.Checked.Equals(true))
            {
                type = "Food";
            }
            if (rdnBtnClothes.Checked.Equals(true))
            {
                type = "Clothes";
            }
            if (rdnBtnBlanket.Checked.Equals(true))
            {
                type = "Blanket";
            }

            ////generate random number
            //int num = 10000;
            //num = num + count;

            //Initialize boolean values to check if each field is empty
            bool empty = false;
            if (empty == false)
            {
                //PhysicalAids p = new PhysicalAids(type, itemName, itemQuantity, itemDesc, num);
                //AllFormClass.PhysicalAids.Add(p);
                dbmanager.AddPhysicalAid(type, itemName, itemQuantity, itemDesc);
            }            
            MessageBox.Show("Item Added Successfully!");
            tbxItemNameAddPhyscialAid.Clear(); 
            tbxQuantity.Clear();
            rdnBtnFood.Checked = false;
            rdnBtnClothes.Checked = false;
            rdnBtnBlanket.Checked = false;
            rtbDescriptionAddPhyscialAid.Clear();

            
        }

        public static void UpdatePhysicalAid(TextBox tbxItemNameUpdatePhysicalAid, TextBox tbxQuantityUpdatePhysicalAid, RichTextBox rtbDescriptionUpdatePhysicalAid,
        RadioButton radioButtonUpdateFood, RadioButton radioButtonUpdateClothes, RadioButton radioButtonUpdateBlacket, TextBox textBox1)
        {
                 
                foreach (PhysicalAids PhysicalAids in AllFormClass.PhysicalAids)
                {
                    //check ur id
                    if (Convert.ToString(PhysicalAids.ItemID) == textBox1.Text)
                    {
                        PhysicalAids.ItemName = tbxItemNameUpdatePhysicalAid.Text;
                        PhysicalAids.Quantity = tbxQuantityUpdatePhysicalAid.Text;
                        PhysicalAids.Description = rtbDescriptionUpdatePhysicalAid.Text;

                        if (radioButtonUpdateFood.Checked.Equals(true))
                        {
                            PhysicalAids.Type = "Food";
                        }
                        if (radioButtonUpdateClothes.Checked.Equals(true))
                        {
                            PhysicalAids.Type = "Clothes";
                        }
                        if (radioButtonUpdateBlacket.Checked.Equals(true))
                        {
                            PhysicalAids.Type = "Blacket";
                        }

                        
                    }
                    dbmanager.UpdatePhysicalAid(PhysicalAids.ItemID, PhysicalAids.ItemName, PhysicalAids.Type, PhysicalAids.Quantity, PhysicalAids.Description);
                }
                dbmanager.ViewPhysicalAid();  
                MessageBox.Show("Physical Aid Updated Successfully!");
            }

        public static void DeletePhysicalAid(ArrayList a1, DataGridView dgvDeletePhysicalAid)
        {

            //DataGridViewSelectedRowCollection tmpdgv = dgvDeletePhysicalAid.SelectedRows;

            //int todeleteindex = Convert.ToInt32(tmpdgv[0].Cells[5].Value.ToString());
 
            ArrayList integer = new ArrayList(); 
            foreach (DataGridViewRow row in dgvDeletePhysicalAid.SelectedRows)
            {

                integer.Add(row.Cells[0].Value);
            }

            for (int i = AllFormClass.PhysicalAids.Count - 1; i >= 0; i--)
            {
                PhysicalAids pa = (PhysicalAids)AllFormClass.PhysicalAids[i];
                for (int j = 0; j < integer.Count; j++)
                {
                    if (pa.ItemID == Convert.ToInt32(integer[j]))
                    {
                        dbmanager.deletePhysicalAid(pa.ItemName);
                    }
                }
                    
            }
            dbmanager.ViewPhysicalAid();
            try
            {
                dgvDeletePhysicalAid.DataSource = null;
                dgvDeletePhysicalAid.Refresh();
                if (AllFormClass.PhysicalAids.Count > 0)
                {
                    dgvDeletePhysicalAid.DataSource = AllFormClass.PhysicalAids;
                    dgvDeletePhysicalAid.Refresh();
                }

            }
            catch (NullReferenceException nre)
            {
                return;
            }
        }

        public static void ViewPhysicalAid(DataGridView dgvViewAllPhysicalAidManagement)
        {
            dbmanager.ViewPhysicalAid();
            try
            {
                dgvViewAllPhysicalAidManagement.DataSource = null;
                dgvViewAllPhysicalAidManagement.Refresh();
                if (AllFormClass.PhysicalAids.Count > 0)
                {
                    dgvViewAllPhysicalAidManagement.DataSource = AllFormClass.PhysicalAids;
                    dgvViewAllPhysicalAidManagement.Refresh();
                }
            }
            catch (NullReferenceException nre)
            {
                return;
            }
        }
    }
}

